package com.zhangxin.designmode.fiveprinciple.openclosedprinciple.before;

/**
 * @author zhangxin
 * @date 2022/4/9
 */
public class Alert {

    private AlertRule rule;
    private Notification notification;

    public Alert(AlertRule rule, Notification notification) {
        this.rule = rule;
        this.notification = notification;
    }


    public void check(String api, long requestCount, long errorCount, long durationOfSeconds,
                      //增加新需求 超时请求数量
                      long timeoutCount
    ) {
        long tps = requestCount / durationOfSeconds;
        if (tps > rule.getMatchedRule(api).getMaxTps()) {
            notification.notify(NotificationEmergencyLevel.URGENCY.getInfo(), "...");
        }
        if (errorCount > rule.getMatchedRule(api).getMaxErrorCount()) {
            notification.notify(NotificationEmergencyLevel.SEVERE.getInfo(), "...");
        }
        //增加接口超时处理逻辑
        long timeoutTps = timeoutCount / durationOfSeconds;
        if (timeoutTps > rule.getMatchedRule(api).getMaxTimeoutTps()) {
            notification.notify(NotificationEmergencyLevel.URGENCY.getInfo(), "...");
        }

    }
}
